package com.xxl.filter;

import com.xxl.util.ConstantField;
import org.springframework.core.annotation.Order;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 身份认证，例如管理员、普通用户之类的
 * @author xxl
 * @date 2023/4/4
 */
@Order(521)
@WebFilter(filterName = "AuthenticationFilter",urlPatterns = {"/admin/*"},description = "身份认证过滤器")
public class AuthenticationFilter extends AuthorizationAndAuthenticationBaseFilter {
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletResponse resp = (HttpServletResponse) response;
        HttpServletRequest rep = (HttpServletRequest) request;
        super.doFilter(request,response,chain);
        String s = userDto.getRoleDto().getRoleName().split(":")[1];
        //等于admin就放行
        if (ConstantField.IDENTITY_ADMIN.equals(s)) {
            chain.doFilter(request,response);
            return;
        }
        resp.sendRedirect(rep.getContextPath()+"/noAuth/NoAuthorization");
    }
}
